package com.hxzy.controller.user;

import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.PageResp;
import com.hxzy.common.resp.R;
import com.hxzy.common.wrap.PageInfoUtil;
import com.hxzy.controller.mapstruct.UserConvert;
import com.hxzy.controller.user.req.UserAddReq;
import com.hxzy.controller.user.req.UserEditReq;
import com.hxzy.controller.user.req.UserReq;
import com.hxzy.controller.user.resp.UserResp;
import com.hxzy.entity.UserinfoWy;
import com.hxzy.service.UserinfoWyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Desc 用户管理
 * @Author: admin
 * @Date: 2023/08/24/9:14
 */
@RestController
@RequestMapping(value = "/api/user")
@Api(tags = "用户管理API")
public class UserController {

    @Autowired
    private UserinfoWyService userinfoWyService;

    @ApiOperation(value = "分页查询")
    @ApiOperationSupport(order = 1)
    @GetMapping(value = "/data")
    public R<PageResp<UserResp>> data( @Valid UserReq  req){
        PageInfo<UserResp> pageInfo=this.userinfoWyService.searchPage(req);
        //返回结果
        return R.okHasData(PageInfoUtil.pageInfoToResp(pageInfo));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增")
    @PostMapping
    public R addSave(@RequestBody @Valid UserAddReq  userAddReq){
        //确认密码
        if(!userAddReq.getPassword().equals(userAddReq.getConfirmPwd())){
            return R.build(AckCode.SET_PASSWORD_INVALID);
        }
        //验证手机号唯一性
        int result=this.userinfoWyService.existsMobile(userAddReq.getMobile());
        if(result>0){
            return R.build(AckCode.EXISTS_VALUE_PHONE);
        }

        //密码加密
        String md5Pwd= BCrypt.hashpw(userAddReq.getPassword());
        userAddReq.setConfirmPwd(md5Pwd);

        //对象转换
        UserinfoWy  entity=UserConvert.INSTALCE.convert(userAddReq);

        boolean success = this.userinfoWyService.insert(entity);

        return success? R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<UserResp> findById(@PathVariable(value = "id") Long id){
        UserinfoWy db = this.userinfoWyService.selectByPrimaryKey(id);
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        //转换对象
        UserResp  resp=UserConvert.INSTALCE.convert(db);
        return R.okHasData(resp);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改")
    @PutMapping
    public R  editSave(@RequestBody @Valid UserEditReq req){
        UserinfoWy db = this.userinfoWyService.selectByPrimaryKey(req.getId());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        //直接修改的值，赋值给db，使用spring框架反射对象copy,更新所有的字段
        //BeanUtils.copyProperties( req,db);
        //this.userinfoWyService.updateByPrimaryKey(db);

        //只更新修改的字段
        UserinfoWy entity=UserConvert.INSTALCE.convert(req);
        boolean success = this.userinfoWyService.updateByPrimaryKeySelective(entity);
        return success? R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    ///api/user/146,145
    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "删除")
    public R remove(@PathVariable(value = "ids") Long[] ids){
        if(ids.length>0){
            boolean success=this.userinfoWyService.deleteBatch(ids);
            return success? R.ok(): R.build(AckCode.DATABASE_ERROR);
        }
        return R.build(AckCode.SYSTEM_PARAM_FAIL);
    }

}
